<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Order_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 保存订单信息
	 */
	public function create($order) {
		$this->db->insert ( 't_order', $order );
		return $this->db->insert_id();//返回插入的ID
	}
	/*
	 *  通过订单ID得到订单信息
	 */
	public function load($id){//
		if (!$id){
			return array();
		}
		$this->db->where('id',$id);
		$query = $this->db->get('t_order');
		if ($row = $query->row_array()){
			return $row;
		}
		return array();
	}
	/*
	 * 更新订单信息
	 */
	public function update($id,$order){
		$this->db->where('id', $id);
		$this->db->update('t_order', $order);
		return $this->db->affected_rows()>0;
	}
	/*
	 * 通过用户ID和订单状态查找订单
	 */
	public function find_by_user_state($userid,$state) {
		$this->db->where('userid',$userid);
		if($state!=-1){
			$this->db->where('state',$state);
		}
		$this->db->where('del=0');
		$this->db->order_by("id", "desc");
		$query = $this->db->get('t_order');
		return $query->result_array();
	}
	
	
	/*
	 * 根据用户和时间段得到营养信息
	 */
	public function count_by_date($userid,$startdate,$enddate){
		$this->db->select('sum(danbaizhi*t_orderdetail.amount*t_compose.amount)/100 as danbaizhi,sum(zhifang*t_orderdetail.amount*t_compose.amount)/100 as zhifang,sum(tanshui*t_orderdetail.amount*t_compose.amount)/100 as tanshui,sum(nengliang*t_orderdetail.amount*t_compose.amount)/100 as nengliang');
		$this->db->from('t_order');
		$this->db->join('t_orderdetail', 't_order.id = t_orderdetail.orderid','left');
		$this->db->join('t_compose', 't_orderdetail.productid=t_compose.productid','left');
		$this->db->join('t_nutrition', 't_compose.materialid = t_nutrition.id','left');
		$this->db->where('userid',$userid);
		$this->db->where('date>=',$startdate);
		$this->db->where('date<=',$enddate);
		$this->db->where('del=0');
		$query = $this->db->get();
		return $query->row_array();
	}
	/*
	 * 根据用户和日期得到营养信息
	 */
	public function find_by_date($userid,$date){
		$this->db->select('(danbaizhi*t_orderdetail.amount*t_compose.amount)/100 as danbaizhi,(zhifang*t_orderdetail.amount*t_compose.amount)/100 as zhifang,(tanshui*t_orderdetail.amount*t_compose.amount)/100 as tanshui,(nengliang*t_orderdetail.amount*t_compose.amount)/100 as nengliang');
		$this->db->from('t_order');
		$this->db->join('t_orderdetail', 't_order.id = t_orderdetail.orderid','left');
		$this->db->join('t_compose', 't_orderdetail.productid=t_compose.productid','left');
		$this->db->join('t_nutrition', 't_compose.materialid = t_nutrition.id','left');
		$this->db->where('userid',$userid);
		$this->db->where('date',$date);
		$this->db->where('del=0');
		$query = $this->db->get();
		return $query->row_array();
	}
	/*
	 * 根据订单编号得到订单信息
	 */
	public function find_by_orderno($orderno) {
		$this->db->where('orderno',$orderno);
		$query = $this->db->get('t_order');
		return $query->row_array();
	}
	/*
	 * 通过用户ID和起始日期查找订单
	 */
	public function find_by_user_date($userid,$startdate,$enddate) {
		$this->db->where('del=0');
		$this->db->where('userid',$userid);
		$this->db->where("DATE_FORMAT(order_time,'%Y-%m-%d')>=",$startdate);
		$this->db->where("DATE_FORMAT(order_time,'%Y-%m-%d')<=",$enddate);
		$query = $this->db->get('t_order');
		return $query->row_array();
	}
	/*
	 * 通过用户ID返回本周内下的订单
	 */
	public function find_by_user($userid) {
		$this->db->where('del=0');
		$this->db->where('userid',$userid);
		$this->db->where("YEARWEEK(date_format(order_time,'%Y-%m-%d')) = YEARWEEK(now())");
		$query = $this->db->get('t_order');
		return $query->row_array();
	}
	/*
	 *根据订单ID 删除订单信息
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_order');
		return $this->db->affected_rows()>0;
	}
}
